package com.fxiao.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.fxiao.db.DBUtils;
import com.fxiao.db.ResultSetHandler;
import com.fxiao.entity.T_User;

public class UserDao {

	/**
	 * 登录的实现方法 (1:用户名错误;2:密码错误;3:正确)
	 * 
	 * @param user_id
	 * @param user_pass
	 * @return
	 */
	public String login(String user_id, String user_pass) {
		String sql = "SELECT user_pass FROM t_user WHERE user_id = ?";
		Object obj = DBUtils.executeQuery(sql, user_id);
		if (obj == null) {
			return "1";// 用户名错误
		} else {
			if (obj.toString().equals(user_pass)) {
				return "3";// 正确
			} else {
				return "2";// 密码错误
			}
		}
	}

	/**
	 * 添加会员
	 * 
	 * @param user
	 * @return
	 */
	public boolean add(T_User user) {
		String sql = "INSERT INTO t_user (user_level, user_state, user_sign, user_id, "
				+ "user_pass, pass_faq, pass_answer, user_name, user_email, safe_pass, safe_faq, "
				+ "safe_answer, user_phone, user_code, user_country, user_sex, user_weixin, user_alipay, "
				+ "user_bank, bank_name, bank_account, bank_id, user_address, user_qq, parent_id, "
				+ "left_num, right_num, level_num, creator, created_time, editor, edit_time,"
				+ "contactor, CPbalance, CPmoney, location) "
				+ "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		return DBUtils.executeUpdate(sql, user.getUser_level(),
				user.getUser_state(), user.getUser_sign(), user.getUser_id(),
				user.getUser_pass(), user.getPass_faq(), user.getPass_answer(),
				user.getUser_name(), user.getUser_email(), user.getSafe_pass(),
				user.getSafe_faq(), user.getSafe_answer(),
				user.getUser_phone(), user.getUser_code(),
				user.getUser_country(), user.getUser_sex(),
				user.getUser_weixin(), user.getUser_alipay(),
				user.getUser_bank(), user.getBank_name(),
				user.getBank_account(), user.getBank_id(),
				user.getUser_address(), user.getUser_qq(), user.getParent_id(),
				user.getLeft_num(), user.getRight_num(), user.getLevel_num(),
				user.getCreator(), user.getCreated_time(), user.getEditor(),
				user.getEdit_time(), user.getContactor(), user.getCPbalance(),
				user.getCPmoney(), user.getLocation()) > 0;
	}

	/**
	 * 根据用户帐号获取用户信息
	 * 
	 * @param user_id
	 * @return
	 */
	public T_User getUserByUserId(final String user_id) {
		String sql = "SELECT user_pass, creator, contactor, user_level, CPbalance, CPmoney, "
				+ "location, user_email, user_name, user_code, user_sex, user_country, "
				+ "user_phone, user_bank, bank_name, bank_account, user_alipay, safe_pass"
				+ " FROM t_user WHERE user_id = ?";
		return (T_User) DBUtils.executeQuery(sql, new ResultSetHandler() {
			public Object doHandler(ResultSet rs) throws SQLException {
				if (rs.next()) {
					T_User user = new T_User();
					user.setUser_pass(rs.getString(1));
					user.setCreator(rs.getString(2));
					user.setContactor(rs.getString(3));
					user.setUser_level(rs.getInt(4));
					user.setCPbalance(rs.getFloat(5));
					user.setCPmoney(rs.getFloat(6));
					user.setLocation(rs.getInt(7));
					user.setUser_email(rs.getString(8));
					user.setUser_name(rs.getString(9));
					user.setUser_code(rs.getString(10));
					user.setUser_sex(rs.getInt(11));
					user.setUser_country(rs.getInt(12));
					user.setUser_phone(rs.getString(13));
					user.setUser_bank(rs.getInt(14));
					user.setBank_name(rs.getString(15));
					user.setBank_account(rs.getString(16));
					user.setUser_alipay(rs.getString(17));
					user.setSafe_pass(rs.getString(18));
					user.setUser_id(user_id);
					return user;
				}
				return null;
			}
		}, user_id);
	}

	/**
	 * 修改保存用户信息
	 */
	public boolean saveUserInfo(T_User user) {
		String sql = "UPDATE t_user SET user_pass=?, CPbalance=?, CPmoney=?, user_email=?, user_name=?,"
				+ "user_code=?, user_sex=?, user_country=?, user_phone=?, user_bank=?, bank_name=?, "
				+ "bank_account=?, user_alipay=?, safe_pass=? "
				+ "WHERE user_id=?";
		return DBUtils.executeUpdate(sql, user.getUser_pass(),
				user.getCPbalance(), user.getCPmoney(), user.getUser_email(),
				user.getUser_name(), user.getUser_code(), user.getUser_sex(),
				user.getUser_country(), user.getUser_phone(),
				user.getUser_bank(), user.getBank_name(),
				user.getBank_account(), user.getUser_alipay(),
				user.getSafe_pass(), user.getUser_id()) > 0;
	}
}
